/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFileChooserInternalFrame.java
 *
 * Created on 24.11.2009, 08:03:53
 */
package GUI.iFrames;

import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.TransferHandler;

/**
 * 
 * @author Bunkowski
 */
public class JFileChooserInternalFrame extends javax.swing.JInternalFrame {

	public static final long serialVersionUID = 1L;

	private HashMap<String, String> JumpTags = new HashMap<String, String>();

	/** Creates new form JFileChooserInternalFrame */
	public JFileChooserInternalFrame() {
		initComponents();
		try {
			ObjectInputStream oi = new ObjectInputStream(new FileInputStream("iphex_jumptags.save"));
			JumpTags = (HashMap<String, String>) oi.readObject();
			oi.close();
			for (String s : JumpTags.keySet()) {
				jComboBox1.addItem(s);
			}

		} catch (Exception ex) {
		}

		jFileTree1.setDragEnabled(true);
		jFileTree1.setTransferHandler(new TransferHandler() {

			public static final long serialVersionUID = 1L;

			public int getSourceActions(JComponent comp) {
				return COPY;
			}

			public Transferable createTransferable(JComponent comp) {
				if (jFileTree1.getSelectFile() != null) {
					return new StringSelection(jFileTree1.getAllSelectedFilesSeparatedByTab());
				} else {
					return new StringSelection("empty selection");
				}
			}
		});

	}

	/**
	 * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
	 * content of this method is always regenerated by the Form Editor.
	 */
	private void initComponents() {

		pop = new javax.swing.JPopupMenu();
		jMenuItem1 = new javax.swing.JMenuItem();
		jMenuItem2 = new javax.swing.JMenuItem();
		jMenu1 = new javax.swing.JMenu();
		jMenuItem3 = new javax.swing.JMenuItem();
		addButton = new javax.swing.JButton();
		jComboBox1 = new javax.swing.JComboBox();
		jScrollPane1 = new javax.swing.JScrollPane();
		jFileTree1 = new toolz.JFileTree();
		delButton = new javax.swing.JButton();
		updateButton = new javax.swing.JButton();
		helpLabel = new javax.swing.JLabel();

		jMenuItem1.setText("jMenuItem1");
		pop.add(jMenuItem1);

		jMenuItem2.setText("jMenuItem2");
		pop.add(jMenuItem2);

		jMenu1.setText("jMenu1");

		jMenuItem3.setText("jMenuItem3");
		jMenu1.add(jMenuItem3);

		pop.add(jMenu1);

		setBackground(new java.awt.Color(255, 255, 255));
		setIconifiable(true);
		setMaximizable(true);
		setResizable(true);
		setTitle("Open files");

		addButton.setBackground(new java.awt.Color(255, 255, 255));
		addButton.setText("add");
		addButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
		addButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				addButtonActionPerformed(evt);
			}
		});

		jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Jump marks" }));
		jComboBox1.setBorder(null);
		jComboBox1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jComboBox1ActionPerformed(evt);
			}
		});

		jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
		jScrollPane1.setViewportView(jFileTree1);

		delButton.setBackground(new java.awt.Color(255, 255, 255));
		delButton.setText("del");
		delButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
		delButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				delButtonActionPerformed(evt);
			}
		});

		updateButton.setIcon(new ImageIcon(JFileChooserInternalFrame.class.getResource("/icons/update.png"))); // NOI18N
		updateButton.setToolTipText("Update file system view");
		updateButton.setMargin(new java.awt.Insets(1, 2, 1, 2));
		updateButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				updateButtonActionPerformed(evt);
			}
		});

		helpLabel.setText("Drag IMS or XLS files to the IPHEx Desktop.");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 29,
										javax.swing.GroupLayout.PREFERRED_SIZE).addGap(0, 0, 0)
								.addComponent(jComboBox1, 0, 149, Short.MAX_VALUE).addGap(1, 1, 1)
								.addComponent(delButton).addGap(0, 0, 0).addComponent(addButton).addGap(0, 0, 0))
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(helpLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addGroup(
								layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
										.addGroup(
												layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(addButton,
																javax.swing.GroupLayout.PREFERRED_SIZE, 26,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jComboBox1,
																javax.swing.GroupLayout.PREFERRED_SIZE, 24,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(delButton,
																javax.swing.GroupLayout.PREFERRED_SIZE, 26,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addComponent(updateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 27,
												javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(1, 1, 1)
						.addComponent(helpLabel).addGap(3, 3, 3)
						.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jComboBox1ActionPerformed
		if (!jComboBox1.getSelectedItem().toString().equals("Jump marks")) {
			try {
				File f = new File(JumpTags.get(jComboBox1.getSelectedItem().toString()));
				System.out.println("Jumping to " + JumpTags.get(jComboBox1.getSelectedItem().toString()));
				jFileTree1.setSelectFile(f);
			} catch (Exception ex) {
				Logger.getLogger(JFileChooserInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
	}// GEN-LAST:event_jComboBox1ActionPerformed

	private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_addButtonActionPerformed
		JumpTags.put(jFileTree1.getSelectFile().getName(), jFileTree1.getSelectFile().toString());
		jComboBox1.addItem(jFileTree1.getSelectFile().getName());
		try {
			ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("iphex_jumptags.save"));
			oo.writeObject(JumpTags);
			oo.close();
		} catch (IOException ex) {
		}

	}// GEN-LAST:event_addButtonActionPerformed

	private void delButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_delButtonActionPerformed
		if (!jComboBox1.getSelectedItem().toString().equals("Jump marks")) {
			JumpTags.remove(jComboBox1.getSelectedItem());
			jComboBox1.removeItem(jComboBox1.getSelectedItem());
			try {
				ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream("iphex_jumptags.save"));
				oo.writeObject(JumpTags);
				oo.close();

			} catch (IOException ex) {
			}
		}
	}// GEN-LAST:event_delButtonActionPerformed

	private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_updateButtonActionPerformed
		try {
			File temp = jFileTree1.getSelectFile();
			jFileTree1.updateFileView();
			jFileTree1.setSelectFile(temp);
		} catch (Exception ex) {
			Logger.getLogger(JFileChooserInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
		}
	}// GEN-LAST:event_updateButtonActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton addButton;
	private javax.swing.JButton delButton;
	private javax.swing.JLabel helpLabel;
	private javax.swing.JComboBox jComboBox1;
	public static toolz.JFileTree jFileTree1;
	private javax.swing.JMenu jMenu1;
	private javax.swing.JMenuItem jMenuItem1;
	private javax.swing.JMenuItem jMenuItem2;
	private javax.swing.JMenuItem jMenuItem3;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JPopupMenu pop;
	private javax.swing.JButton updateButton;
	// End of variables declaration//GEN-END:variables
}
